﻿using System.Runtime.InteropServices;

namespace HobSoft.SoccerBet2014.Common.Net
{
    public class NetTools
    {
        private const long INTERNET_CONNECTION_MODEM = 1;
        private const long INTERNET_CONNECTION_LAN = 2;
        private const long INTERNET_CONNECTION_PROXY = 4;
        private const long INTERNET_CONNECTION_MODEM_BUSY = 8;
        private const long INTERNET_RAS_INSTALLED = 16;
        private const long INTERNET_CONNECTION_OFFLINE = 32;
        private const long INTERNET_CONNECTION_CONFIGURED = 64;
        [DllImport("wininet.dll")]
        protected static extern bool InternetGetConnectedState(out long lpdwFlags, long dwReserved);

        public static bool HasConnection()
        {
            long flag;
            return InternetGetConnectedState(out flag, 0);
        }

        public static bool Ping(string hostOrAddress)
        {
            System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
            System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
            options.DontFragment = true;
            byte[] buffer = new byte[32];
            int timeout = 1000;
            var pingReply = p.Send(hostOrAddress, timeout, buffer);
            if (pingReply.Status == System.Net.NetworkInformation.IPStatus.Success)
                return true;
            else
                return false;
        }
    }
}
